package com.zhuzhe.blog.article.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhuzhe.blog.article.requestDto.ArticleDto;
import com.zhuzhe.blog.article.requestDto.ArticleSerachDto;
import com.zhuzhe.blog.article.requestDto.ArticleThumhupDto;
import com.zhuzhe.blog.article.requestDto.ArticleUserDto;
import com.zhuzhe.blog.article.responseVo.ArticlePageListVo;
import com.zhuzhe.blog.article.responseVo.ArticleVo;
import com.zhuzhe.blog.article.service.IArticleService;
import com.zhuzhe.blog.base.ResponseData;
import com.zhuzhe.blog.enums.ArticleStatusEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 文章信息表 前端控制器
 * </p>
 *
 * @author u53
 * @since 2020-10-05
 */
@RestController
@RequestMapping("/article")
@Api(value="文章管理接口", description = "文章管理接口，提供文章的增、删、改、查")
public class ArticleController {

    @Autowired
    private IArticleService articleService;

    // 在请求方法之前会校验用户是否有对应的权限，如果有则可以调用到此方法，没有则调用失败
    @PreAuthorize("hasAuthority('article:search')")
    @ApiOperation("根据文章标题与状态查询文章列表接口")
    @PostMapping("/serach")
    public ResponseData<IPage<ArticlePageListVo>> serach(@RequestBody ArticleSerachDto articleDto){
        return articleService.queryPage(articleDto);
    }

    @ApiImplicitParams({
                    @ApiImplicitParam(name = "id", value = "文章ID", required = true),
                    @ApiImplicitParam(name = "userId", value = "用户id")
            })
    @ApiOperation("根据id查询文章详情")
    @GetMapping("/{id}}")
    public ResponseData<ArticleVo>searchById(@PathVariable String id, String userId){
        return articleService.findArticleAndLabelById(id,userId);
    }

    @ApiOperation("修改文章信息接口")
    @PutMapping
    public ResponseData<String> update(@RequestBody ArticleDto articleDto) {
        return articleService.updateOrSave(articleDto);
    }

    @ApiOperation("新增文章信息接口")
    @PostMapping
    public ResponseData<String> save(@RequestBody ArticleDto articleDto) {
        return articleService.updateOrSave(articleDto);
    }

    @ApiImplicitParam(name = "id", value = "文章ID", required = true)
    @ApiOperation("删除文章接口")
    @DeleteMapping("/{id}")
    public ResponseData<String> delete(@PathVariable("id") String id) {
        // 假删除，只是将状态更新
        return articleService.updateStatus(id, ArticleStatusEnum.DELETE);
    }

    @ApiImplicitParam(name = "id", value = "文章ID", required = true)
    @ApiOperation("审核通过接口")
    @GetMapping("/audit/success/{id}")
    public ResponseData<String> success(@PathVariable("id") String id) {
        // 审核通过
        return articleService.updateStatus(id, ArticleStatusEnum.SUCCESS);
    }

    @ApiImplicitParam(name = "id", value = "文章ID", required = true)
    @ApiOperation("审核不通过接口")
    @GetMapping("/audit/fail/{id}")
    public ResponseData<String> fail(@PathVariable("id") String id) {
        // 审核不通过
        return articleService.updateStatus(id, ArticleStatusEnum.FAIL);
    }

    @ApiOperation("根据用户ID查询公开或未公开的文章列表接口")
    @PostMapping("/user")
    public ResponseData<IPage<ArticlePageListVo>> findListByUserId(@RequestBody ArticleUserDto articleUserDto) {
        return articleService.findListByUserId(articleUserDto);
    }


    @ApiOperation("更新点赞数")
    @PostMapping("/thumb")
    public ResponseData<String> updataThumhup(@RequestBody ArticleThumhupDto articleThumhupDto) {
        return articleService.updateThumhup(articleThumhupDto);
    }

    @ApiOperation("统计审核通过且公开的文章总记录数")
    @GetMapping("/total")
    public ResponseData<Integer> getArticleTotal() {
        return articleService.getArticleTotal();
    }

    @ApiOperation("统计各分类下的文章数")
    @GetMapping("/category/total")
    public ResponseData<Map> categoryTotal() {
        return articleService.selectCategoryTotal();
    }

    @ApiOperation("统计近6个月发布的文章数")
    @GetMapping("/month/total")
    public ResponseData<Map> monthArticleTotal() {
        return articleService.selectMonthArticleTotal();
    }

}
